package com.team.six.bluecatshop.controller;


import com.team.six.bluecatshop.domain.Admin;
import com.team.six.bluecatshop.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     */
    @GetMapping("/adminLogin")
    public String adminLoginShow(){
        return "administration/admin_login";
    }

    /**
     * 处理登录请求
     */
    @PostMapping("/adminLogin")
    public String adminLogin(Admin admin, HttpSession session){
        Admin admin1 = adminService.findAdminByName(admin.getAdminName());
        if(admin1!= null){
            if(admin1.getAdminPassword().equals(admin.getAdminPassword())){
                //System.out.println(admin.getAdminPassword());
                //System.out.println(admin1.getAdminPassword());
                session.setAttribute("adminName",admin1.getAdminName());
                return "redirect:/adminProduct/productList";
            }else {
                //System.out.println(admin.getAdminPassword());
                //System.out.println(admin1.getAdminPassword());
                System.out.println("密码错误,请重新登录");
                return "redirect:/admin/adminLogin";
            }
        }else {
            System.out.println("该用户已经存在");
            return "redirect:/admin/adminLogin";
        }
    }
}
